<?php
include_once("../module/checkA.php");
include_once("../../config.php");

$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';

$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '0';


/* 权限判断 */
admin_priv('weixin');

if($act == ''){
	$url = 'detail-list.php';
	header('Location:'.$url);
	exit; 
}

if($id == ''){
	$url = 'detail-list.php';
	header('Location:'.$url);
	exit; 
}


if($act == 'add' || $act == 'edit'){
			
	$next_act = $act == 'add' ? 'insert' : 'update';
	
	
	$mp_list = select_lines_all_items($table_Prefix.'wechat_official','nid desc',"","");

	$new_mp_list = array();
	foreach($mp_list as $key=>$val){
		$new_mp_list[$val['nid']]['nid'] = $val['nid'];
		$new_mp_list[$val['nid']]['name'] = $val['name'];
	}
	
	unset($mp_list);
}

if($act == 'edit'){
	
	$detail = select_one_line_all_items($table_Prefix.'wechat_themes', "t_id = '$id' ");
	
	$themes_info = unserialize($detail['themes_info']);
	
	
}
elseif($act == 'update'){
	
	$table    = $table_Prefix ."wechat_themes";

	
	$themes_name = trim($_REQUEST['themes_name']) ;
	$themes_url = trim($_REQUEST['themes_url']) ;
	$themes_sn = trim($_REQUEST['themes_sn']) ;
	
	$mp_id = intval($_REQUEST['mp_id']) ;
	
	$t_id =  $_REQUEST['t_id'] ? intval($_REQUEST['t_id']) : 0 ;

	$themes_info = array();
	
	$themes_info['keywords_name'] = $_REQUEST['keywords_name'];
	$themes_info['keywords_color'] = $_REQUEST['keywords_color'];
	$themes_info['keywords_desc'] = $_REQUEST['keywords_desc'];
	
	if($t_id <= 0){
		echo '<script type="text/javascript">alert("信息有误，请重新输入");window.location.href="detail-list-themes.php?nav-itemed=wechat";</script>';
		exit;
	}
	
	$sql = "UPDATE `$table` SET `themes_name`= '$themes_name' , `themes_url`= '$themes_url',`themes_sn`= '$themes_sn',`mp_id`= '$mp_id',`themes_info`= '".serialize($themes_info)."' WHERE t_id = '".$t_id."' ";
	
	$res = do_query($sql);

	if($res){
		echo '<script type="text/javascript">alert("修改成功");window.location.href="detail-list-themes.php?nav-itemed=wechat";</script>';
	}
	exit;
		
}
elseif($act == 'insert'){

	$themes_name = trim($_REQUEST['themes_name']) ;
	$themes_url = trim($_REQUEST['themes_url']) ;
	$themes_sn = trim($_REQUEST['themes_sn']) ;
	
	$mp_id = intval($_REQUEST['mp_id']) ;

	$themes_info = array();
	
	$themes_info['keywords_name'] = $_REQUEST['keywords_name'];
	$themes_info['keywords_color'] = $_REQUEST['keywords_color'];
	$themes_info['keywords_desc'] = $_REQUEST['keywords_desc'];
	
	$sql = "INSERT INTO ".$table_Prefix."wechat_themes"." ( `themes_name`, `themes_sn`, `themes_info`, `mp_id`, `themes_url`) VALUES ('$themes_name','$themes_sn','".serialize($themes_info)."','$mp_id','$themes_url')";
	
	$res = do_query($sql);

	if($res){
		echo '<script type="text/javascript">alert("新增成功");window.location.href="detail-list-themes.php?nav-itemed=wechat";</script>';
	}
	exit;
}
elseif($act == 'delete'){
	
	$detail = select_one_line_all_items($table_Prefix.'wechat_themes', "t_id = '$id' ");

	if($detail){
		$sql = 'DELETE FROM '.$table_Prefix.'wechat_themes'.' WHERE t_id ='.$id;
		
		$res = do_query($sql);
		
		if($res){
			echo '<script type="text/javascript">alert("删除成功");window.location.href="detail-list-themes.php?nav-itemed=wechat";</script>';
			exit;
		}
		else{
			echo '<script type="text/javascript">alert("删除失败");window.location.href="detail-lis-themest.php?nav-itemed=wechat";</script>';
			exit;
		}
	}
	else{
		echo '<script type="text/javascript">alert("删除成功");window.location.href="detail-list-themes.php?nav-itemed=wechat";</script>';
			
		exit;
	}
	
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include_once(ROOT_PATH_.'/controlbase/module/head.php'); ?>

</head>

<body>

<div id="topbar"><?php include ROOT_PATH_.'/controlbase/module/topbar.php'; ?></div>

<div id="container" class="<? echo $system_part ?>">

    <div id="maincontent">

        <div id="sidebar">
            <?php include ROOT_PATH_.'/controlbase/module/sidebar.php'; ?>
        </div>


		<div id="content" class="layui-form">
			<h1> 模板信息 </h1>

			<div class="tips">
				
			</div>
			<div class="detail-box member-edit-box layui-form">

                <div class="detail-boxes">
					<div class="detail-boxing">

					<form id="edit-member-form" method="post" action="detail_info_themes.php?act=<?php echo $next_act;?>" class="layui-form">
 						  <input type="hidden" name="t_id" value="<? echo $detail['t_id']?>">
  
                          
                          <div class="layui-form-item">
                            <label class="layui-form-label">公众号选择</label>
                            <div class="layui-input-block">
                               <select name="mp_id"  >
                                 <option value="">请选择公众号</option>
                                 <?php 
								 foreach($new_mp_list as $val){
								 ?>
                                 <option value="<?php echo $val['nid'];?>" <?php if($detail['mp_id'] == $val['nid']){echo 'selected="selected"';}?> ><?php echo $val['name']?></option>
                                 <?php 
								 }
								 ?>
                               </select>
                             
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <label class="layui-form-label">模板编号</label>
                            <div class="layui-input-block">
                              <input type="text" name="themes_sn" lay-verify="required" required placeholder="请输入模板编号" autocomplete="off" class="layui-input" value="<? echo $detail['themes_sn']?>">
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <label class="layui-form-label">模板标题</label>
                            <div class="layui-input-block">
                              <input type="text" name="themes_name" lay-verify="required" require   placeholder="请输入模板标题" autocomplete="off" class="layui-input" value="<? echo $detail['themes_name']?>">
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <label class="layui-form-label">模板链接</label>
                            <div class="layui-input-block">
                              <input type="text" name="themes_url"   placeholder="请输入模板链接" autocomplete="off" class="layui-input" value="<? echo $detail['themes_url']?>">
                            </div>
                          </div>
                          <div class="layui-form-item">
                            <label class="layui-form-label">字体颜色代码选择</label>
                            <div class="layui-input-block">
                              <div id="color"></div>
                            </div>
                          </div>
                          
                          <?php 
						  if($themes_info){
							 $count=count($themes_info['keywords_color']);
							 
							 
							 for($i=0;$i<$count;$i++){
								 
						  ?>	 
							
                          <div class="themes_info_box">
                              <div class="layui-form-item layui-anim">
                                <label class="layui-form-label <?php echo $i == ($count-1) ?  'delete_box' : 'add_box'?>" ><i class="layui-icon <?php echo $i == ($count-1) ?  'layui-icon-close' : 'layui-icon-add-1'?>"></i>模板内容</label>
                                <div class="layui-input-block">
                                   内容字段名：<input type="text" name="keywords_name[]" lay-verify="required" required class="layui-input"   placeholder="请输入模板内容字段名，first.DATA，填写  first " autocomplete="off"  value="<? echo $themes_info['keywords_name'][$i]?>"><br />
                                   内容颜色：<input type="text" name="keywords_color[]" class="layui-input"   placeholder="请输入颜色代码，默认为黑色 " autocomplete="off"  value="<? echo $themes_info['keywords_color'][$i]?>"><br />
                                   内容内容：<textarea name="keywords_desc[]" placeholder="请输入内容,输入内容后自动默认取当前内容为主"   class="layui-textarea"><? echo $themes_info['keywords_desc'][$i]?></textarea><br />
                                </div>
                              </div>
						  </div>
                          <?php 
						    }
							  
						  }
						  else{
						  ?>
                           <div class="themes_info_box">
                              <div class="layui-form-item layui-anim">
                                <label class="layui-form-label add_box" ><i class="layui-icon layui-icon-add-1"></i>模板内容</label>
                                <div class="layui-input-block">
                                   内容字段名：<input type="text" name="keywords_name[]" lay-verify="required" required class="layui-input"   placeholder="请输入模板内容字段名，first.DATA，填写  first " autocomplete="off"  value=""><br />
                                   内容颜色：<input type="text" name="keywords_color[]" class="layui-input"   placeholder="请输入颜色代码，默认为黑色 " autocomplete="off"  value=""><br />
                                   内容内容：<textarea name="keywords_desc[]" placeholder="请输入内容,输入内容后自动默认取当前内容为主"   class="layui-textarea"></textarea><br />
                                </div>
                              </div>
						  </div>
                          <?php }?>
                          <div class="layui-form-item">
                            <div class="layui-input-block">
                              <button class="layui-btn" lay-submit lay-filter="formMemberAdd" type="submit">立即提交</button>
                              <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                          </div>

					</form>
					</div>
				</div>
			</div>

<!--content端點01-->
</div>
<!--maincontent端點01-->
<br class="clearfloat"/>
</div>
<!--container端點01-->
</div>

<script>

$('.themes_info_box').on("click", ".add_box",function(){
	info = $(this).parent().clone()
	info.find('.layui-icon').removeClass('layui-icon-add-1').addClass('layui-icon-close');
	info.find('.layui-form-label').removeClass('add_box').addClass('delete_box');
	info.addClass('layui-anim-upbit');
	
	
	
	
	$(this).parent().before(info);
	
	layui.use('form', function(){
 		 var form = layui.form;
  		form.render(); //更新全部
  
	});
	
})

$('.themes_info_box').on("click", ".delete_box",function(){
	
	$(this).parent().remove();
	layui.use('form', function(){
 		 var form = layui.form;
  		form.render(); //更新全部
  
	});
})

layui.use('colorpicker', function(){
  var colorpicker = layui.colorpicker;
  //渲染
  colorpicker.render({
    elem: '#color'  //绑定元素
  });
});
</script>
<?php include_once('../module/footer.php'); ?> </body>
</html>
